package com.cangxuan.manage.controller;

import com.cangxuan.manage.annotation.check.Max;
import com.cangxuan.manage.annotation.check.NotNull;
import com.cangxuan.manage.service.IAdminService;
import com.cangxuan.manage.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;


import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@Api(tags = "管理员相关接口")
@RestController
@RequestMapping("admin")
public class AdminController extends BaseController{
    IAdminService adminService;
    @Autowired
    public void setAdminService(IAdminService adminService) {
        this.adminService = adminService;
    }
    @Data
    @ApiModel("密码修改body")
    public static class ChangePasswordBody{
        @ApiModelProperty("旧密码")
        @NotNull(message = "请输入旧密码")
        String oldPwd;
        @ApiModelProperty("新密码")
        @NotNull(message = "请输入新密码")
        String newPwd;
        @ApiModelProperty("确认新密码")
        @NotNull(message = "请再次输入新密码")
        String ConfirmPwd;

    }
    @PostMapping("changePassword")
    @ApiOperation("修改管理员密码")
    public Object changePassword ( @RequestBody ChangePasswordBody changePasswordBody) {
        if (!changePasswordBody.getNewPwd().equals(changePasswordBody.getConfirmPwd())) {
            return fail(808, "两次新密码输入不一致");
        }
            boolean result = adminService.changePassword(SecurityUtils.md5("admin"+changePasswordBody.getOldPwd()), SecurityUtils.md5("admin"+changePasswordBody.getConfirmPwd()));
        if (result) {
            return success("修改成功");
        } else {
            return fail(809,"修改失败,密码错误");
        }
    }
}
